home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 12 regular expressions / regextester / form1.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  16.4 KB  |  382 lines

  1. Imports System.IO
  2. Imports System.Text.RegularExpressions
  3.  
  4. Public Class Form1
  5.     Inherits System.Windows.Forms.Form
  6.  
  7. #Region " Windows Form Designer generated code "
  8.  
  9.     Public Sub New()
  10.         MyBase.New()
  11.  
  12.         'This call is required by the Windows Form Designer.
  13.         InitializeComponent()
  14.  
  15.         'Add any initialization after the InitializeComponent() call
  16.  
  17.     End Sub
  18.  
  19.     'Form overrides dispose to clean up the component list.
  20.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  21.         If disposing Then
  22.             If Not (components Is Nothing) Then
  23.                 components.Dispose()
  24.             End If
  25.         End If
  26.         MyBase.Dispose(disposing)
  27.     End Sub
  28.     Friend WithEvents Label1 As System.Windows.Forms.Label
  29.     Friend WithEvents Label2 As System.Windows.Forms.Label
  30.                                 Friend WithEvents Label3 As System.Windows.Forms.Label
  31.             Friend WithEvents Label4 As System.Windows.Forms.Label
  32.         Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
  33.     Friend WithEvents txtSource As System.Windows.Forms.TextBox
  34.     Friend WithEvents txtRegex As System.Windows.Forms.TextBox
  35.         Friend WithEvents btnLoad As System.Windows.Forms.Button
  36.     Friend WithEvents btnFind As System.Windows.Forms.Button
  37.     Friend WithEvents btnSplit As System.Windows.Forms.Button
  38.     Friend WithEvents btnReplace As System.Windows.Forms.Button
  39.     Friend WithEvents txtReplace As System.Windows.Forms.TextBox
  40.     Friend WithEvents chkMultiline As System.Windows.Forms.CheckBox
  41.     Friend WithEvents chkIgnoreCase As System.Windows.Forms.CheckBox
  42.     Friend WithEvents txtResult As System.Windows.Forms.TextBox
  43.     Friend WithEvents chkShowGroups As System.Windows.Forms.CheckBox
  44.     Friend WithEvents chkShowCaptures As System.Windows.Forms.CheckBox
  45.     Friend WithEvents lblResultCount As System.Windows.Forms.Label
  46.     
  47.     'Required by the Windows Form Designer
  48.     Private components As System.ComponentModel.Container
  49.  
  50.     'NOTE: The following procedure is required by the Windows Form Designer
  51.     'It can be modified using the Windows Form Designer.  
  52.     'Do not modify it using the code editor.
  53.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  54.         Me.btnSplit = New System.Windows.Forms.Button()
  55.         Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
  56.         Me.Label4 = New System.Windows.Forms.Label()
  57.         Me.txtResult = New System.Windows.Forms.TextBox()
  58.         Me.Label2 = New System.Windows.Forms.Label()
  59.         Me.Label3 = New System.Windows.Forms.Label()
  60.         Me.btnFind = New System.Windows.Forms.Button()
  61.         Me.btnReplace = New System.Windows.Forms.Button()
  62.         Me.btnLoad = New System.Windows.Forms.Button()
  63.         Me.chkShowGroups = New System.Windows.Forms.CheckBox()
  64.         Me.chkMultiline = New System.Windows.Forms.CheckBox()
  65.         Me.txtSource = New System.Windows.Forms.TextBox()
  66.         Me.txtRegex = New System.Windows.Forms.TextBox()
  67.         Me.txtReplace = New System.Windows.Forms.TextBox()
  68.         Me.Label1 = New System.Windows.Forms.Label()
  69.         Me.chkIgnoreCase = New System.Windows.Forms.CheckBox()
  70.         Me.chkShowCaptures = New System.Windows.Forms.CheckBox()
  71.         Me.lblResultCount = New System.Windows.Forms.Label()
  72.         Me.SuspendLayout()
  73.         '
  74.         'btnSplit
  75.         '
  76.         Me.btnSplit.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
  77.         Me.btnSplit.Location = New System.Drawing.Point(288, 72)
  78.         Me.btnSplit.Name = "btnSplit"
  79.         Me.btnSplit.Size = New System.Drawing.Size(64, 32)
  80.         Me.btnSplit.TabIndex = 2
  81.         Me.btnSplit.Text = "&Split"
  82.         '
  83.         'Label4
  84.         '
  85.         Me.Label4.Location = New System.Drawing.Point(8, 248)
  86.         Me.Label4.Name = "Label4"
  87.         Me.Label4.Size = New System.Drawing.Size(72, 24)
  88.         Me.Label4.TabIndex = 0
  89.         Me.Label4.Text = "Results"
  90.         '
  91.         'txtResult
  92.         '
  93.         Me.txtResult.AcceptsReturn = True
  94.         Me.txtResult.AcceptsTab = True
  95.         Me.txtResult.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
  96.                     Or System.Windows.Forms.AnchorStyles.Left) _
  97.                     Or System.Windows.Forms.AnchorStyles.Right)
  98.         Me.txtResult.Location = New System.Drawing.Point(8, 272)
  99.         Me.txtResult.Multiline = True
  100.         Me.txtResult.Name = "txtResult"
  101.         Me.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Both
  102.         Me.txtResult.Size = New System.Drawing.Size(600, 104)
  103.         Me.txtResult.TabIndex = 4
  104.         Me.txtResult.Text = ""
  105.         '
  106.         'Label2
  107.         '
  108.         Me.Label2.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
  109.         Me.Label2.Location = New System.Drawing.Point(376, 16)
  110.         Me.Label2.Name = "Label2"
  111.         Me.Label2.Size = New System.Drawing.Size(248, 24)
  112.         Me.Label2.TabIndex = 0
  113.         Me.Label2.Text = "Replace Pattern"
  114.         '
  115.         'Label3
  116.         '
  117.         Me.Label3.Location = New System.Drawing.Point(8, 112)
  118.         Me.Label3.Name = "Label3"
  119.         Me.Label3.Size = New System.Drawing.Size(248, 24)
  120.         Me.Label3.TabIndex = 0
  121.         Me.Label3.Text = "Text to be parsed"
  122.         '
  123.         'btnFind
  124.         '
  125.         Me.btnFind.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
  126.         Me.btnFind.Location = New System.Drawing.Point(288, 40)
  127.         Me.btnFind.Name = "btnFind"
  128.         Me.btnFind.Size = New System.Drawing.Size(64, 32)
  129.         Me.btnFind.TabIndex = 2
  130.         Me.btnFind.Text = "&Find"
  131.         '
  132.         'btnReplace
  133.         '
  134.         Me.btnReplace.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
  135.         Me.btnReplace.Location = New System.Drawing.Point(608, 40)
  136.         Me.btnReplace.Name = "btnReplace"
  137.         Me.btnReplace.Size = New System.Drawing.Size(80, 32)
  138.         Me.btnReplace.TabIndex = 2
  139.         Me.btnReplace.Text = "&Replace"
  140.         '
  141.         'btnLoad
  142.         '
  143.         Me.btnLoad.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
  144.         Me.btnLoad.Location = New System.Drawing.Point(608, 136)
  145.         Me.btnLoad.Name = "btnLoad"
  146.         Me.btnLoad.Size = New System.Drawing.Size(80, 32)
  147.         Me.btnLoad.TabIndex = 2
  148.         Me.btnLoad.Text = "&Load"
  149.         '
  150.         'chkShowGroups
  151.         '
  152.         Me.chkShowGroups.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
  153.         Me.chkShowGroups.CheckAlign = System.Drawing.ContentAlignment.TopLeft
  154.         Me.chkShowGroups.Checked = True
  155.         Me.chkShowGroups.CheckState = System.Windows.Forms.CheckState.Checked
  156.         Me.chkShowGroups.Font = New System.Drawing.Font("Microsoft Sans Serif", 10!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  157.         Me.chkShowGroups.Location = New System.Drawing.Point(616, 272)
  158.         Me.chkShowGroups.Name = "chkShowGroups"
  159.         Me.chkShowGroups.Size = New System.Drawing.Size(80, 40)
  160.         Me.chkShowGroups.TabIndex = 5
  161.         Me.chkShowGroups.Text = "Show Groups"
  162.         Me.chkShowGroups.TextAlign = System.Drawing.ContentAlignment.TopLeft
  163.         '
  164.         'chkMultiline
  165.         '
  166.         Me.chkMultiline.Location = New System.Drawing.Point(144, 80)
  167.         Me.chkMultiline.Name = "chkMultiline"
  168.         Me.chkMultiline.Size = New System.Drawing.Size(88, 24)
  169.         Me.chkMultiline.TabIndex = 3
  170.         Me.chkMultiline.Text = "&Multiline"
  171.         '
  172.         'txtSource
  173.         '
  174.         Me.txtSource.AcceptsReturn = True
  175.         Me.txtSource.AcceptsTab = True
  176.         Me.txtSource.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
  177.                     Or System.Windows.Forms.AnchorStyles.Right)
  178.         Me.txtSource.Location = New System.Drawing.Point(8, 136)
  179.         Me.txtSource.Multiline = True
  180.         Me.txtSource.Name = "txtSource"
  181.         Me.txtSource.ScrollBars = System.Windows.Forms.ScrollBars.Both
  182.         Me.txtSource.Size = New System.Drawing.Size(600, 104)
  183.         Me.txtSource.TabIndex = 4
  184.         Me.txtSource.Text = ""
  185.         '
  186.         'txtRegex
  187.         '
  188.         Me.txtRegex.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
  189.                     Or System.Windows.Forms.AnchorStyles.Right)
  190.         Me.txtRegex.Location = New System.Drawing.Point(8, 40)
  191.         Me.txtRegex.Name = "txtRegex"
  192.         Me.txtRegex.Size = New System.Drawing.Size(280, 26)
  193.         Me.txtRegex.TabIndex = 1
  194.         Me.txtRegex.Text = ""
  195.         '
  196.         'txtReplace
  197.         '
  198.         Me.txtReplace.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
  199.         Me.txtReplace.Location = New System.Drawing.Point(376, 40)
  200.         Me.txtReplace.Name = "txtReplace"
  201.         Me.txtReplace.Size = New System.Drawing.Size(232, 26)
  202.         Me.txtReplace.TabIndex = 1
  203.         Me.txtReplace.Text = ""
  204.         '
  205.         'Label1
  206.         '
  207.         Me.Label1.Location = New System.Drawing.Point(8, 16)
  208.         Me.Label1.Name = "Label1"
  209.         Me.Label1.Size = New System.Drawing.Size(248, 24)
  210.         Me.Label1.TabIndex = 0
  211.         Me.Label1.Text = "Regular Expression Pattern"
  212.         '
  213.         'chkIgnoreCase
  214.         '
  215.         Me.chkIgnoreCase.Location = New System.Drawing.Point(8, 80)
  216.         Me.chkIgnoreCase.Name = "chkIgnoreCase"
  217.         Me.chkIgnoreCase.Size = New System.Drawing.Size(120, 24)
  218.         Me.chkIgnoreCase.TabIndex = 3
  219.         Me.chkIgnoreCase.Text = "&Ignore case"
  220.         '
  221.         'chkShowCaptures
  222.         '
  223.         Me.chkShowCaptures.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
  224.         Me.chkShowCaptures.CheckAlign = System.Drawing.ContentAlignment.TopLeft
  225.         Me.chkShowCaptures.Font = New System.Drawing.Font("Microsoft Sans Serif", 10!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  226.         Me.chkShowCaptures.Location = New System.Drawing.Point(616, 336)
  227.         Me.chkShowCaptures.Name = "chkShowCaptures"
  228.         Me.chkShowCaptures.Size = New System.Drawing.Size(80, 40)
  229.         Me.chkShowCaptures.TabIndex = 5
  230.         Me.chkShowCaptures.Text = "Show Captures"
  231.         Me.chkShowCaptures.TextAlign = System.Drawing.ContentAlignment.TopLeft
  232.         '
  233.         'lblResultCount
  234.         '
  235.         Me.lblResultCount.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  236.         Me.lblResultCount.ForeColor = System.Drawing.Color.RoyalBlue
  237.         Me.lblResultCount.Location = New System.Drawing.Point(88, 248)
  238.         Me.lblResultCount.Name = "lblResultCount"
  239.         Me.lblResultCount.Size = New System.Drawing.Size(520, 24)
  240.         Me.lblResultCount.TabIndex = 0
  241.         '
  242.         'Form1
  243.         '
  244.         Me.AutoScaleBaseSize = New System.Drawing.Size(8, 19)
  245.         Me.ClientSize = New System.Drawing.Size(696, 381)
  246.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblResultCount, Me.chkShowCaptures, Me.chkShowGroups, Me.txtResult, Me.Label4, Me.btnLoad, Me.txtSource, Me.Label3, Me.btnReplace, Me.chkMultiline, Me.chkIgnoreCase, Me.btnSplit, Me.btnFind, Me.txtReplace, Me.txtRegex, Me.Label2, Me.Label1})
  247.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  248.         Me.MinimumSize = New System.Drawing.Size(704, 408)
  249.         Me.Name = "Form1"
  250.         Me.Text = "Regex Tester"
  251.         Me.ResumeLayout(False)
  252.  
  253.     End Sub
  254.  
  255. #End Region
  256.  
  257.     ' ask the user which file should be loaded
  258.     Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
  259.         With openFileDialog1()
  260.             .DefaultExt = "TXT"
  261.             .CheckFileExists = True
  262.             .Filter = "(Text files (*.txt)|*.txt|All files (*.*)|*.*"
  263.             .FilterIndex = 0
  264.             If .ShowDialog = DialogResult().OK Then
  265.                 ' Read the text file
  266.                 txtSource().Text = FileText(.FileName)
  267.             End If
  268.         End With
  269.     End Sub
  270.  
  271.     ' Find matches
  272.  
  273.     Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
  274.         ' find matches
  275.         Dim re As Regex
  276.         Dim reOpt As RegexOptions = GetRegexOptions()
  277.  
  278.         Try
  279.             ' attempt to create the RegEx object
  280.             re = New Regex(txtRegex().Text, reOpt)
  281.         Catch ex As Exception
  282.             ' if any error, display it and exit
  283.             MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  284.             Exit Sub
  285.         End Try
  286.  
  287.         ' get the collection of results
  288.         Dim mc As MatchCollection = re.Matches(txtSource().Text)
  289.         ' display number of matches
  290.         lblResultCount.Text = "Found " & mc.Count.ToString & " matches."
  291.         txtResult.Clear()
  292.  
  293.         Dim m As Match
  294.         For Each m In mc
  295.             ShowRes("'" & m.Value & "' at index " & m.Index)
  296.             If m.Groups.Count > 1 And chkShowGroups.Checked = True Then
  297.                 Dim g As Group, i As Integer
  298.                 ' skip the 0th group, which is the entire Match object
  299.                 For i = 1 To m.Groups.Count - 1
  300.                     ' Get a reference to the corresponding group.
  301.                     g = m.Groups(i)
  302.                     ShowRes(String.Format("   group({0}) = '{1}'", i, g.Value))
  303.                     If chkShowCaptures.Checked = True Then
  304.                         ' Get the capture collection for this group.                
  305.                         Dim cc As CaptureCollection = g.Captures
  306.                         ' Display information on each capture.
  307.                         Dim c As Capture
  308.                         For Each c In cc
  309.                             ShowRes(String.Format("      capture '{0}' at index {1}", c.Value, c.Index))
  310.                         Next
  311.                     End If
  312.                 Next
  313.             End If
  314.         Next
  315.     End Sub
  316.  
  317.     ' retrieve RegEx options
  318.  
  319.     Function GetRegexOptions() As RegexOptions
  320.         ' retrieve options
  321.         If chkIgnoreCase().Checked Then GetRegexOptions = RegexOptions.IgnoreCase
  322.         If chkMultiline().Checked Then
  323.             GetRegexOptions = GetRegexOptions Or RegexOptions.Multiline
  324.         End If
  325.     End Function
  326.  
  327.     ' Read the contents of a text file.
  328.  
  329.     Function FileText(ByVal path As String) As String
  330.         ' Open a file stream and define a stream reader.
  331.         Dim fs As New FileStream(path, FileMode.Open)
  332.         Dim sr As New StreamReader(fs)
  333.         ' Read the entire contents of this file.
  334.         FileText = sr.ReadToEnd
  335.         ' Clean-up code.
  336.         sr.Close()
  337.         fs.Close()
  338.     End Function
  339.  
  340.     ' display data in the Results pane
  341.  
  342.     Sub ShowRes(ByVal msg As String, Optional ByVal ClearIt As Boolean = False)
  343.         If ClearIt Then txtResult().Clear()
  344.         txtResult().AppendText(msg & ControlChars.CrLf)
  345.     End Sub
  346.  
  347.     ' replace pattern
  348.  
  349.     Private Sub btnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click
  350.         ' very concise syntax
  351.         Try
  352.             txtResult().Text = Regex.Replace(txtSource().Text, txtRegex().Text, txtReplace().Text, GetRegexOptions())
  353.         Catch ex As Exception
  354.             ' if any error, display it 
  355.             MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  356.         End Try
  357.     End Sub
  358.  
  359.     ' split the source
  360.  
  361.     Private Sub btnSplit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSplit.Click
  362.         Dim re As Regex
  363.  
  364.         Try
  365.             re = New Regex(txtRegex().Text, GetRegexOptions())
  366.         Catch ex As Exception
  367.             ' if any error, display it 
  368.             MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  369.         End Try
  370.  
  371.         ' split the input string
  372.         Dim sArr() As String = re.Split(txtSource().Text)
  373.         ShowRes("Found " & sArr.Length & " items.", True)
  374.  
  375.         Dim s As String
  376.         For Each s In sArr
  377.             ShowRes(s)
  378.         Next
  379.     End Sub
  380.  
  381. End Class
  382.